
package my.algorithms.mcorrea.topcoder;

/**
 *You are studying for the final exam in a tough course, and want to know how many points
 * you need to score on the final to pass the course. You know how many points you earned 
 * on each assignment (pointsEarned), how many points were possible on each assignment 
 * (pointsPossible), and how many points are possible on the final exam (finalExam). 
 * You need to earn a minimum of 65% of the total possible points to pass the course. 
 * Assume your score on the final exam will be an integral number of points between 0 
 * and finalExam, inclusive. Return the number of points you need to score on the final 
 * to pass the course, or -1 if it is impossible for you to pass the course.
 * @author mcorrea
 */
public class PassingGrade {
	
	
	public static void main(String args[]){
		int[] pointsEarned = {55,77,82,60};
		int[] pointsPossible = {100,100,100,100};
		int finalExam = 300;
		System.out.println( pointsNeeded(pointsEarned, pointsPossible, finalExam) );
		
		int[] pointsEarned1 = { 1, 2, 3, 4 };
		int[] pointsPossible1 = { 2, 3, 4, 5 };
		finalExam = 7;
		System.out.println( pointsNeeded(pointsEarned1, pointsPossible1, finalExam) );
		
		int[] pointsEarned2 = { 1, 2, 2, 1 };
		int[] pointsPossible2 = { 9, 9, 9, 9 };
		finalExam = 9;
		System.out.println( pointsNeeded(pointsEarned2, pointsPossible2, finalExam) );
		
		int[] pointsEarned3 = { 7, 8, 7, 6 };
		int[] pointsPossible3 = { 8, 8, 8, 8 };
		finalExam = 9;
		System.out.println( pointsNeeded(pointsEarned3, pointsPossible3, finalExam) );
		
		int[] pointsEarned4 = { 17, 23, 50, 200, 19, 56, 83, 91, 77, 9, 0 };
		int[] pointsPossible4 = { 20, 30, 50, 250, 20, 70, 100, 100, 100, 10, 10 };
		finalExam = 400;
		System.out.println( pointsNeeded(pointsEarned4, pointsPossible4, finalExam) );
		
		int[] pointsEarned5 = {600,600,600,600,600,600,600,600,600,600,
 600,600,600,600,600,600,600,600,600,600};
		int[] pointsPossible5 = {1000,1000,1000,1000,1000,1000,1000,1000,
 1000,1000,1000,1000,1000,1000,1000,1000,
 1000,1000,1000,901};
		finalExam = 3000;
		System.out.println( pointsNeeded(pointsEarned5, pointsPossible5, finalExam) );
	
	}
	
	public static int pointsNeeded(int[] pointsEarned, 
			int[] pointsPossible, int finalExam){
		
		int pointsNeeded = 0;
	    int sumPossible = 0;
		int sumEarned =0;
		for(int k=0;k<pointsEarned.length; k++){
			sumPossible +=pointsPossible[k];
			sumEarned +=pointsEarned[k];
		}
		sumPossible +=finalExam;
		pointsNeeded = (int)Math.round( (double)(0.65*sumPossible - sumEarned) );
		
		if(pointsNeeded > finalExam){
			return -1; //cannot make it
		}
		
		if(pointsNeeded <= 0){
			return 0;//does not need poitns
		}
		return pointsNeeded;
		
	}
	
	
}
